package com.jelly.web.share.home;

import com.jelly.common.constant.MenuConstant;
import com.jelly.common.constant.MessageConstant;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.message.service.IMessageService;
import com.jelly.web.modules.monitor.log.service.IOperLogService;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.modules.basics.bulletin.service.IBulletinAndUserService;
import com.jelly.web.modules.system.dept.service.IDeptService;
import com.jelly.web.modules.staff.user.domain.User;
import com.jelly.web.modules.staff.user.domain.UserAndRoleAndPermission;
import com.jelly.web.modules.staff.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @创建人 ql
 * @创建时间 2018-07-31 16:54
 * @功能描述: 后台主页控制器
 **/

@Controller
public class AdminController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IBulletinAndUserService bulletinAndUserService;

    @Autowired
    private IOperLogService operLogService;

    @Autowired
    private IMessageService messageService;

    /**
     *  后台主页跳转
     * @param model
     * @return
     */
    @GetMapping("/")
    String getAdmin(Model model){

        //获取当前登陆的用户信息
        User user = TokenUtil.getToken();

        model.addAttribute("user",user);
        //当前用户的未读消息
        model.addAttribute("msgCount",messageService.getMessageReadCount(MessageConstant.READNO));
        //当前用户的部门信息
        model.addAttribute("dept",deptService.getDeptId(user.getDeptId()));
        //获取当前用户可访问的导航详情
        UserAndRoleAndPermission userAndRoleAndPermission = userService.getUserAndRoleAndPermission(user.getUserId(),MenuConstant.MENUTYPE,null);

        if(null != userAndRoleAndPermission){

            model.addAttribute("menu",userAndRoleAndPermission.getPermissionList());

        }
        return "/index";

    }

    /**
     * 首页详情
     * @param model
     * @return
     */
    @GetMapping("/share/welcome")
    String getIndexHome(Model model){

        //公告通知
        model.addAttribute("bulletin",bulletinAndUserService.getBulletinAndUserAll(null));
        getDictionary();

        return share("/welcome");

    }

    /**
     * 首页操作记录
     * @param model
     * @return
     */
    @GetMapping("/share/operation")
    String getOperation(Model model){

        //当前用户的操作记录
        model.addAttribute("operLog",operLogService.getOperLogUserId());

        return adminUserInfor("/operation");
    }

    /**
     *  无权访问
     * @return
     */
    @GetMapping("/noAuthority.html")
    String noPermission(){
        return "/error/noAuthority";
    }

}
